import {Injectable} from '@angular/core';
import {Salary} from '../types/resp/Salary';
import {SalaryForm} from '../types/req/SalaryForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {SalaryQueryForm} from '../types/req/SalaryQueryForm';

@Injectable({
  providedIn: 'root'
})
export class SalaryService {
  salarys: Salary[] = [
    {"id":1,"employeeId":1,"employeeName":"张伟","basicSalary":15000.00,"performanceSalary":5000.00,"bonus":3000.00,"allowance":2000.00,"overtimePay":1200.00,"socialSecurityDeduction":2500.00,"housingFundDeduction":1800.00,"personalIncomeTaxDeduction":1500.00,"otherDeductions":500.00,"grossSalary":26200.00,"netSalary":19900.00,"payDate":"2024-11-27","paymentMethod":10,"bankAccountNumber":"6222021234567890123","createTime":"2024-04-03 22:24:18","modifyTime":"2024-12-18 22:24:18"},
    {"id":2,"employeeId":4,"employeeName":"陈静","basicSalary":18000.00,"performanceSalary":6000.00,"bonus":4000.00,"allowance":2500.00,"overtimePay":1500.00,"socialSecurityDeduction":3000.00,"housingFundDeduction":2000.00,"personalIncomeTaxDeduction":2000.00,"otherDeductions":800.00,"grossSalary":32000.00,"netSalary":24700.00,"payDate":"2024-09-10","paymentMethod":30,"bankAccountNumber":"N/A","createTime":"2024-10-21 22:24:18","modifyTime":"2024-07-12 22:24:18"},
    {"id":3,"employeeId":2,"employeeName":"李娜","basicSalary":12000.00,"performanceSalary":4000.00,"bonus":2000.00,"allowance":1500.00,"overtimePay":1000.00,"socialSecurityDeduction":2000.00,"housingFundDeduction":1500.00,"personalIncomeTaxDeduction":1200.00,"otherDeductions":400.00,"grossSalary":20500.00,"netSalary":15400.00,"payDate":"2024-05-16","paymentMethod":10,"bankAccountNumber":"6222029876543210987","createTime":"2024-12-02 22:24:18","modifyTime":"2024-01-11 22:24:18"},
    {"id":4,"employeeId":3,"employeeName":"王强","basicSalary":20000.00,"performanceSalary":7000.00,"bonus":5000.00,"allowance":3000.00,"overtimePay":2000.00,"socialSecurityDeduction":3500.00,"housingFundDeduction":2500.00,"personalIncomeTaxDeduction":2500.00,"otherDeductions":1000.00,"grossSalary":37500.00,"netSalary":29000.00,"payDate":"2024-12-14","paymentMethod":30,"bankAccountNumber":"N/A","createTime":"2024-03-17 22:24:18","modifyTime":"2024-08-19 22:24:18"},
    {"id":5,"employeeId":1,"employeeName":"张伟","basicSalary":15000.00,"performanceSalary":5000.00,"bonus":3000.00,"allowance":2000.00,"overtimePay":1200.00,"socialSecurityDeduction":2500.00,"housingFundDeduction":1800.00,"personalIncomeTaxDeduction":1500.00,"otherDeductions":500.00,"grossSalary":26200.00,"netSalary":19900.00,"payDate":"2024-02-27","paymentMethod":20,"bankAccountNumber":"zhangwei@alipay.com","createTime":"2024-08-22 22:24:18","modifyTime":"2024-09-26 22:24:18"},
    {"id":6,"employeeId":1,"employeeName":"张伟","basicSalary":15000.00,"performanceSalary":5000.00,"bonus":3000.00,"allowance":2000.00,"overtimePay":1200.00,"socialSecurityDeduction":2500.00,"housingFundDeduction":1800.00,"personalIncomeTaxDeduction":1500.00,"otherDeductions":500.00,"grossSalary":26200.00,"netSalary":19900.00,"payDate":"2024-09-22","paymentMethod":30,"bankAccountNumber":"N/A","createTime":"2024-10-03 22:24:18","modifyTime":"2024-05-28 22:24:18"}
  ]
  constructor() {
  }

  save (salaryForm: SalaryForm): Observable<Result<void>> {
    console.log('salaryApi save params', salaryForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (salaryQueryForm: SalaryQueryForm): Observable<Result<Page<Salary>>> {
    console.log('salaryApi find params', salaryQueryForm)
    return MockApi.queryPageSuccessfully(this.salarys).pipe(
      map((response: Response<Result<Page<Salary>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Salary>> {
    console.log('salaryApi findById id', id)
    let target = {}
    for (const a of this.salarys) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Salary>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('salaryApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
